﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;


namespace MsDAL
{
   public partial  class Sys8a00Base
    {		
		/// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual  SqlApp Add(Model.Sys8a00 sys8a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Sys8a00(");
			strSql.Append("popno ,ptable ,pkey ,fields ,pagesize ,orderfid ,strwhere ,keyref ,reffield ,issavekey ,description ,width ,height )");
			strSql.Append(" values (");
			strSql.Append("@popno ,@ptable ,@pkey ,@fields ,@pagesize ,@orderfid ,@strwhere ,@keyref ,@reffield ,@issavekey ,@description ,@width ,@height )");		

			SqlParameter[] parameters = {
				new SqlParameter("@popno",SqlDbType.NVarChar),
				new SqlParameter("@ptable",SqlDbType.NVarChar),
				new SqlParameter("@pkey",SqlDbType.NVarChar),
				new SqlParameter("@fields",SqlDbType.NVarChar),
				new SqlParameter("@pagesize",SqlDbType.Int),
				new SqlParameter("@orderfid",SqlDbType.NVarChar),
				new SqlParameter("@strwhere",SqlDbType.NVarChar),
				new SqlParameter("@keyref",SqlDbType.NVarChar),
				new SqlParameter("@reffield",SqlDbType.NVarChar),
				new SqlParameter("@issavekey",SqlDbType.NVarChar),
				new SqlParameter("@description",SqlDbType.NVarChar),
				new SqlParameter("@width",SqlDbType.Int),
				new SqlParameter("@height",SqlDbType.Int)
				};
			parameters[0].Value = sys8a00.Popno;
			parameters[1].Value = sys8a00.Ptable;
			parameters[2].Value = sys8a00.Pkey;
			parameters[3].Value = sys8a00.Fields;
			parameters[4].Value = sys8a00.Pagesize;
			parameters[5].Value = sys8a00.Orderfid;
			parameters[6].Value = sys8a00.Strwhere;
			parameters[7].Value = sys8a00.Keyref;
			parameters[8].Value = sys8a00.Reffield;
			parameters[9].Value = sys8a00.Issavekey;
			parameters[10].Value = sys8a00.Description;
			parameters[11].Value = sys8a00.Width;
			parameters[12].Value = sys8a00.Height;
			
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");
        }
	
        /// <summary>
        /// 判断某个值是否存在表中的某个字段
        /// </summary>
        /// <param name="Condition">条件字段名 例:A=@A</param>
        /// <param name="hs">参数和值 如hs["@A"]=123</param>
        /// <returns></returns>
        public virtual bool Exists(String condition, Hashtable hs)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from Sys8a00 ");
			if (!string.IsNullOrEmpty(condition))
				strSql.Append(" where " + condition);
			List<SqlParameter> list = DBHelper.GetListByHashtable(hs);
			if (list.Count > 0)
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString(), list.ToArray())) > 0;
			else
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString())) > 0;
        }

		/// <summary>
        /// 更新一条数据
        /// </summary>
        public virtual SqlApp Update(Model.Sys8a00 sys8a00)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Sys8a00 set ");
			strSql.Append("ptable=@ptable ,pkey=@pkey ,fields=@fields ,pagesize=@pagesize ,orderfid=@orderfid ,strwhere=@strwhere ,keyref=@keyref ,reffield=@reffield ,issavekey=@issavekey ,description=@description ,width=@width ,height=@height ");
			strSql.Append("where popno=@popno ");
			SqlParameter[] parameters = {
				new SqlParameter("@popno", SqlDbType.NVarChar),
				new SqlParameter("@ptable", SqlDbType.NVarChar),
				new SqlParameter("@pkey", SqlDbType.NVarChar),
				new SqlParameter("@fields", SqlDbType.NVarChar),
				new SqlParameter("@pagesize", SqlDbType.Int),
				new SqlParameter("@orderfid", SqlDbType.NVarChar),
				new SqlParameter("@strwhere", SqlDbType.NVarChar),
				new SqlParameter("@keyref", SqlDbType.NVarChar),
				new SqlParameter("@reffield", SqlDbType.NVarChar),
				new SqlParameter("@issavekey", SqlDbType.NVarChar),
				new SqlParameter("@description", SqlDbType.NVarChar),
				new SqlParameter("@width", SqlDbType.Int),
				new SqlParameter("@height", SqlDbType.Int)
				};
			parameters[0].Value = sys8a00.Popno;
			parameters[1].Value = sys8a00.Ptable;
			parameters[2].Value = sys8a00.Pkey;
			parameters[3].Value = sys8a00.Fields;
			parameters[4].Value = sys8a00.Pagesize;
			parameters[5].Value = sys8a00.Orderfid;
			parameters[6].Value = sys8a00.Strwhere;
			parameters[7].Value = sys8a00.Keyref;
			parameters[8].Value = sys8a00.Reffield;
			parameters[9].Value = sys8a00.Issavekey;
			parameters[10].Value = sys8a00.Description;
			parameters[11].Value = sys8a00.Width;
			parameters[12].Value = sys8a00.Height;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");			
		}
		
		public virtual SqlApp Delete(Model.Sys8a00 sys8a00)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM  Sys8a00  ");
			strSql.Append("WHERE popno=@popno ");
			SqlParameter[] parameters = {
				new SqlParameter("@popno", SqlDbType.NVarChar)
				};
			parameters[0].Value = sys8a00.Popno;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");		
		}
		
		/// <summary>
        /// 根据Popno得到整个 Sys8a00对象,如果没查到，返回null
        /// </summary>
        /// <param name="Popno">该实体主键 Popno</param>
        /// <returns>实体Sys8a00的对象</returns>
		public virtual Model.Sys8a00 GetModelById(string Popno)
        {
			string where = "popno=@popno ";
			
			string sql = GetSql(where,null);
			
			List<SqlParameter> parameters = new List<SqlParameter>();
			
			SqlParameter p0 = new SqlParameter("@Popno",SqlDbType.NVarChar);
			p0.Value = Popno;
			parameters.Add(p0);
			
			DataTable dt = DBHelper.GetData(sql, parameters.ToArray());
			
			//调用实体Sys8a00的GetSys8a00ListByDataTable方法得到实体对象
			List<Model.Sys8a00> list =Model.Sys8a00.GetSys8a00ListByDataTable(dt);
			
			return  (list.Count>0) ? list[0] : null;
        }
		
		
		 /// <summary>
        /// 得到Sql语句
        /// </summary>
        /// <param name="Condition">Sql语句的条件</param>
		/// <param name="OrderBy">排序条件，默认为String.Empty</param>
        /// <returns></returns>
        protected virtual String GetSql(string condition,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [popno] ,[ptable] ,[pkey] ,[fields] ,[pagesize] ,[orderfid] ,[strwhere] ,[keyref] ,[reffield] ,[issavekey] ,[description] ,[width] ,[height] ");
            strSql.Append(" FROM Sys8a00 ");
 			
			if (!string.IsNullOrEmpty(condition)) strSql.Append(" where " + condition);				
			if(!string.IsNullOrEmpty(orderBy)) strSql.Append( " order by " + orderBy );
			
            return  strSql.ToString();
        }

        /// <summary>
        /// 根据条件和参数得到Sys8a00对象
        /// </summary>
        /// <param name="Condition">Sql条件</param>
        /// <param name="hs">参数和值</param>
        /// <returns></returns>
        public virtual Model.Sys8a00 GetModel(String Condition, Hashtable hs)
        {
			String sql = GetSql(Condition,String.Empty);
			DataTable dt = DBHelper.GetTableByCondition(sql,  hs);
			return (dt.Rows.Count > 0) ? Model.Sys8a00.GetSys8a00ListByDataTable(dt)[0] : null;
        }

        /// <summary>
        /// 根据主键的值和某字段得到其值,可以用来判断是否存在
        /// </summary>
        /// <param name="TableKeyValue">主键值</param>
        /// <param name="FieldName">要查询的字段名称</param>
        /// <returns></returns>
        public virtual Object GetValueByField(Object TableKeyValue, Object FieldName)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select " + FieldName + " from Sys8a00 ");
			strSql.Append(" where popno=@TableKeyValue ");
			SqlParameter p = new SqlParameter("@TableKeyValue", TableKeyValue);

			return DBHelper.ExecuteScalar(strSql.ToString(), p);
        }
		
        /// <summary>
        /// 分页SQL
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual ReturnPageData GetList(Pager page)
        {
            ReturnPageData data = new ReturnPageData();
            //page.PageIndex++;
            page.PrimaryKey = "popno ";

            page.ShowFields = " [popno] ,[ptable] ,[pkey] ,[fields] ,[pagesize] ,[orderfid] ,[strwhere] ,[keyref] ,[reffield] ,[issavekey] ,[description] ,[width] ,[height] ";

            page.TableName = " Sys8a00 ";

			page.StrWhere = string.IsNullOrEmpty(page.StrWhere) ? " 1=1 " : page.StrWhere;
			
            page.StrWhere = string.IsNullOrEmpty(page.AutoWhere) ? page.StrWhere : page.StrWhere + " And ("+ page.AutoWhere +")";

            page.For = PagerFor.Count;
            DataTable dtRowCount = DBHelper.GetSelectLimit(page);
            if (dtRowCount == null || dtRowCount.Rows.Count == 0)
            {
                data.AllRowCount = 0;
                return data; //NULL DATA
            }
            data.AllRowCount = Convert.ToInt32(dtRowCount.Rows[0]["Total"]);
            page.For = PagerFor.Data;

            DataTable dtPageData = DBHelper.GetSelectLimit(page);
            data.CurrnetPageData = dtPageData;

            //汇总
            page.For = PagerFor.Summary;
            data.SummaryData = DBHelper.GetSelectLimit(page);
            
            return data;
        }		
		
		public virtual DataTable GetList(string c,Hashtable hs)
		{
			string sql = GetSql(c,null); //得到SQL语句
			//返回结果集
			return DBHelper.GetData(sql,DBHelper.GetListByHashtable(hs).ToArray());
		}
    }
}
